package com.ymatou.query.seller.trading.api.parameter;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ymttest.common.enums.DateTypeEnum;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

public class Sp_GetSellerSalesAmount {
	private YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();

	private Integer sellerId;
	private Boolean paidInFull;
	private int orderSellerAcceptedStatus;
	private List<Integer> orderStatus;
	private Boolean all;
	private String yearBegin;
	private String yearEnd;
	private String quarterBegin;
	private String quarterEnd;
	private String monthBegin;
	private String monthEnd;
	private String weekBegin;
	private String weekEnd;
	private String todayBegin;
	private String todayEnd;
	private String yesterdayBegin;
	private String yesterdayEnd;
	private Boolean considerOrderStatus;
	private Boolean considerRCOrderEstablish;
	private Boolean considerRCAccountPaid;
	private Boolean considerRestOrderStatus;

	public Integer getSellerId() {
		return sellerId;
	}

	public void setSellerId(Integer sellerId) {
		this.sellerId = sellerId;
	}

	public Boolean getPaidInFull() {
		return paidInFull;
	}

	public void setPaidInFull(Boolean paidInFull) {
		this.paidInFull = paidInFull;
	}

	public int getOrderSellerAcceptedStatus() {
		return orderSellerAcceptedStatus;
	}

	public void setOrderSellerAcceptedStatus(int orderSellerAcceptedStatus) {
		this.orderSellerAcceptedStatus = orderSellerAcceptedStatus;
	}

	public List<Integer> getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(List<Integer> orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Boolean getAll() {
		return all;
	}

	public void setAll(Boolean all) {
		this.all = all;
	}

	public String getYearBegin() {
		return yearBegin;
	}

	public void setYearBegin(String yearBegin) {
		this.yearBegin = yearBegin;
	}

	public String getYearEnd() {
		return yearEnd;
	}

	public void setYearEnd(String yearEnd) {
		this.yearEnd = yearEnd;
	}

	public String getQuarterBegin() {
		return quarterBegin;
	}

	public void setQuarterBegin(String quarterBegin) {
		this.quarterBegin = quarterBegin;
	}

	public String getQuarterEnd() {
		return quarterEnd;
	}

	public void setQuarterEnd(String quarterEnd) {
		this.quarterEnd = quarterEnd;
	}

	public String getMonthBegin() {
		return monthBegin;
	}

	public void setMonthBegin(String monthBegin) {
		this.monthBegin = monthBegin;
	}

	public String getMonthEnd() {
		return monthEnd;
	}

	public void setMonthEnd(String monthEnd) {
		this.monthEnd = monthEnd;
	}

	public String getWeekBegin() {
		return weekBegin;
	}

	public void setWeekBegin(String weekBegin) {
		this.weekBegin = weekBegin;
	}

	public String getWeekEnd() {
		return weekEnd;
	}

	public void setWeekEnd(String weekEnd) {
		this.weekEnd = weekEnd;
	}

	public String getTodayBegin() {
		return todayBegin;
	}

	public void setTodayBegin(String todayBegin) {
		this.todayBegin = todayBegin;
	}

	public String getTodayEnd() {
		return todayEnd;
	}

	public void setTodayEnd(String todayEnd) {
		this.todayEnd = todayEnd;
	}

	public String getYesterdayBegin() {
		return yesterdayBegin;
	}

	public void setYesterdayBegin(String yesterdayBegin) {
		this.yesterdayBegin = yesterdayBegin;
	}

	public String getYesterdayEnd() {
		return yesterdayEnd;
	}

	public void setYesterdayEnd(String yesterdayEnd) {
		this.yesterdayEnd = yesterdayEnd;
	}

	public Boolean getConsiderOrderStatus() {
		return considerOrderStatus;
	}

	public void setConsiderOrderStatus(Boolean considerOrderStatus) {
		this.considerOrderStatus = considerOrderStatus;
	}

	public Boolean getConsiderRCOrderEstablish() {
		return considerRCOrderEstablish;
	}

	public void setConsiderRCOrderEstablish(Boolean considerRCOrderEstablish) {
		this.considerRCOrderEstablish = considerRCOrderEstablish;
	}

	public Boolean getConsiderRCAccountPaid() {
		return considerRCAccountPaid;
	}

	public void setConsiderRCAccountPaid(Boolean considerRCAccountPaid) {
		this.considerRCAccountPaid = considerRCAccountPaid;
	}

	public Boolean getConsiderRestOrderStatus() {
		return considerRestOrderStatus;
	}

	public void setConsiderRestOrderStatus(Boolean considerRestOrderStatus) {
		this.considerRestOrderStatus = considerRestOrderStatus;
	}

	@SuppressWarnings("rawtypes")
	public Map<Integer, BigDecimal> execute() {
		Map<Integer, BigDecimal> result = new HashMap<Integer, BigDecimal>();
		String queryField = "dAddTime, fTotalPrice";
		StringBuffer queryCondition = new StringBuffer();
		queryCondition.append(" iBuyerId = " + sellerId);
		if (paidInFull != null || DataUtil.isNullOrEmpty(orderStatus) == false) {
			queryCondition.append(" and (");
			boolean addOr = false;
			if (paidInFull != null) {
				queryCondition.append(" (o.iTradingStatus = "
						+ orderSellerAcceptedStatus + " and o.bPaidInFull = "
						+ DataUtil.bool2Int(paidInFull) + ")");
				addOr = true;
			}
			if (DataUtil.isNullOrEmpty(orderStatus) == false) {
				if (addOr) {
					queryCondition.append(" or");
				}
				queryCondition.append(" o.iTradingStatus in "
						+ DataUtil.toInOfSql(orderStatus));
			}
			queryCondition.append(" )");
		}
		List<Map> results = ymtOrdersWapper.selectOrders2(queryField, null,
				queryCondition.toString());
		if (all) {
			BigDecimal total = new BigDecimal(0);
			for (Map m : results) {
				Iterator keys = m.keySet().iterator();
				while (keys.hasNext()) {
					Object key = keys.next();
					if (key.equals("fTotalPrice")) {
						total = total.add(DataUtil.BigDecimall
								.GetValueOrDefault(m.get(key)));
					}
				}
			}
			result.put(DateTypeEnum.All.getValue(), total);
		}
		if (yearBegin != null) {
			result.put(
					DateTypeEnum.ThisYear.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(yearBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(yearEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		if (quarterBegin != null) {
			result.put(
					DateTypeEnum.ThisQuarter.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(quarterBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(quarterEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		if (monthBegin != null) {
			result.put(
					DateTypeEnum.ThisMonth.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(monthBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(monthEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		if (weekBegin != null) {
			result.put(
					DateTypeEnum.ThisWeek.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(weekBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(weekEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		if (todayBegin != null) {
			result.put(
					DateTypeEnum.Today.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(todayBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(todayEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		if (yesterdayBegin != null) {
			result.put(
					DateTypeEnum.Yesterday.getValue(),
					calcTotal(
							results,
							YMTDateUtil.getBeforeOrNextDay(yesterdayBegin, 0,
									YMTDateUtil.YMD) + " 00:00:00",
							YMTDateUtil.getBeforeOrNextDay(yesterdayEnd, 0,
									YMTDateUtil.YMD) + " 00:00:00"));
		}
		return result;
	}

	private BigDecimal calcTotal(List<Map> results, String begin, String end) {
		BigDecimal total = new BigDecimal(0);
		for (Map m : results) {
			String dAddTime = String.valueOf(m.get("dAddTime"));
			int bigger = YMTDateUtil.parseYMDHMSDate(dAddTime).compareTo(
					YMTDateUtil.parseYMDHMSDate(begin));
			int less = YMTDateUtil.parseYMDHMSDate(dAddTime).compareTo(
					YMTDateUtil.parseYMDHMSDate(end));
			if ((bigger == 0 || bigger == 1) && less == -1) {
				BigDecimal fTotalPrice = DataUtil.BigDecimall
						.GetValueOrDefault(m.get("fTotalPrice"));
				total = total.add(fTotalPrice);
			}
		}
		return total;
	}

}
